package org.example.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.ibatis.annotations.*;
import org.example.pojo.Doctor.Doctor;
import org.example.pojo.Doctor.Schedule;
import org.example.pojo.DoctorUser;
import org.example.pojo.Patient.PatientUser;
import org.example.pojo.User;

import java.util.Date;
import java.util.List;

@Mapper
@DS("second")
public interface AdminUserMapper {

    @Select("SELECT u.id,u.username,u.email,u.create_time,p.patient_id,p.name,p.gender,p.phone_number,p.id_card,p.age FROM user u left join patients p on u.username=p.username WHERE u.role = #{role}")
    List<PatientUser> findUsersByRoleP(@Param("role") String role);
    @Select("SELECT u.id, u.username,u.email, u.create_time, " +
            "d.doctor_id, d.name, d.gender, d.title, d.contact_number, d.clinic_id " +
            "FROM user u " +
            "LEFT JOIN doctors d ON u.username = d.username " +
            "WHERE u.role = #{role}")
    List<DoctorUser> findUsersByRoleD(@Param("role") String role);
    @Select("SELECT * FROM patients WHERE patient_id = #{patientId}")
    PatientUser findUserByIdAndRoleP(Integer patientId);
    @Select("SELECT * FROM doctors WHERE doctor_id = #{doctorId}")
    Doctor findUserByIdAndRoleD(Integer doctorId);
    @Update("update doctors set name = #{name}, gender = #{gender}, title = #{title}, contact_number = #{contactNumber}, clinic_id = #{clinicId} where doctor_id = #{doctorId}")
    void updateDoctor(DoctorUser doctor);
    @Update("UPDATE patients SET name = #{name}, gender = #{gender},age=#{age},phone_number=#{phoneNumber},id_card=#{idCard} WHERE patient_id=#{patientId} ")
    void updatePatient(PatientUser patient);
    @Delete("DELETE u,p FROM user u left join patients p on u.username = p.username WHERE id = #{id} AND role = #{role}")
    void deleteUserByIdAndRoleP(@Param("id") Integer id, @Param("role") String role);
    @Delete("DELETE u,d FROM user u " +
    "LEFT JOIN doctors d ON u.username = d.username " +
    "WHERE u.id = #{id} AND u.role = #{role}")
    void deleteUserByIdAndRoleD(@Param("id") Integer id, @Param("role") String role);
    @Select("SELECT * FROM schedules WHERE schedule_date = #{scheduleDate} AND clinic_id = #{clinicId}")
    List<Schedule> getDoctorSchedule(Date scheduleDate, Integer clinicId);
    @Insert("INSERT INTO schedules (doctor_id, clinic_id, schedule_date, day_week, start_time, end_time,doctor_name,clinic_name) " +
            "VALUES (#{doctorId}, #{clinicId}, #{scheduleDate}, #{dayWeek}, #{startTime}, #{endTime},#{doctorName},#{clinicName})")
    void addDoctorSchedule(Schedule schedule);
    @Delete("DELETE FROM schedules WHERE schedule_id = #{scheduleId}")
    void deleteDoctorSchedule(Integer scheduleId);
    @Update("UPDATE schedules SET doctor_id = #{doctorId},doctor_name=#{doctorName},clinic_id = #{clinicId},clinic_name=#{clinicName}, schedule_date = #{scheduleDate}, day_week = #{dayWeek}, start_time = #{startTime}, end_time = #{endTime} WHERE schedule_id = #{scheduleId}")
    void updateDoctorSchedule(Schedule schedule);
    @Select("SELECT * FROM schedules")
    List<Schedule> getDoctorScheduleNull();
    @Select("SELECT * FROM schedules WHERE schedule_date = #{scheduleDate}")
    List<Schedule> getDoctorScheduleByDate(String scheduleDate);
    @Select("SELECT * FROM schedules WHERE clinic_id = #{clinicId}")
    List<Schedule> getDoctorScheduleById(Integer clinicId);
}